<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>UNIX-specific utilities and integration</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="GLib Reference Manual">
<link rel="up" href="glib-utilities.html" title="GLib Utilities">
<link rel="prev" href="glib-Testing.html" title="Testing">
<link rel="next" href="glib-Windows-Compatibility-Functions.html" title="Windows Compatibility Functions">
<meta name="generator" content="GTK-Doc V1.17 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2">
<tr valign="middle">
<td><a accesskey="p" href="glib-Testing.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
<td><a accesskey="u" href="glib-utilities.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
<th width="100%" align="center">GLib Reference Manual</th>
<td><a accesskey="n" href="glib-Windows-Compatibility-Functions.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
</tr>
<tr><td colspan="5" class="shortcuts">
<a href="#glib-UNIX-specific-utilities-and-integration.synopsis" class="shortcut">Top</a>
                   | 
                  <a href="#glib-UNIX-specific-utilities-and-integration.description" class="shortcut">Description</a>
</td></tr>
</table>
<div class="refentry">
<a name="glib-UNIX-specific-utilities-and-integration"></a><div class="titlepage"></div>
<div class="refnamediv"><table width="100%"><tr>
<td valign="top">
<h2><span class="refentrytitle"><a name="glib-UNIX-specific-utilities-and-integration.top_of_page"></a>UNIX-specific utilities and integration</span></h2>
<p>UNIX-specific utilities and integration — pipes, signal handling</p>
</td>
<td valign="top" align="right"></td>
</tr></table></div>
<div class="refsynopsisdiv">
<a name="glib-UNIX-specific-utilities-and-integration.synopsis"></a><h2>Synopsis</h2>
<pre class="synopsis">
#include &lt;glib-unix.h&gt;

#define             <a class="link" href="glib-UNIX-specific-utilities-and-integration.html#G-UNIX-ERROR:CAPS" title="G_UNIX_ERROR">G_UNIX_ERROR</a>
<a class="link" href="glib-Basic-Types.html#gboolean" title="gboolean"><span class="returnvalue">gboolean</span></a>            <a class="link" href="glib-UNIX-specific-utilities-and-integration.html#g-unix-open-pipe" title="g_unix_open_pipe ()">g_unix_open_pipe</a>                    (<em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> *fds</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> flags</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a> **error</code></em>);
<a class="link" href="glib-Basic-Types.html#guint" title="guint"><span class="returnvalue">guint</span></a>               <a class="link" href="glib-UNIX-specific-utilities-and-integration.html#g-unix-signal-add" title="g_unix_signal_add ()">g_unix_signal_add</a>                   (<em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> signum</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-The-Main-Event-Loop.html#GSourceFunc" title="GSourceFunc ()"><span class="type">GSourceFunc</span></a> handler</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gpointer" title="gpointer"><span class="type">gpointer</span></a> user_data</code></em>);
<a class="link" href="glib-Basic-Types.html#guint" title="guint"><span class="returnvalue">guint</span></a>               <a class="link" href="glib-UNIX-specific-utilities-and-integration.html#g-unix-signal-add-full" title="g_unix_signal_add_full ()">g_unix_signal_add_full</a>              (<em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> priority</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> signum</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-The-Main-Event-Loop.html#GSourceFunc" title="GSourceFunc ()"><span class="type">GSourceFunc</span></a> handler</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gpointer" title="gpointer"><span class="type">gpointer</span></a> user_data</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-Datasets.html#GDestroyNotify" title="GDestroyNotify ()"><span class="type">GDestroyNotify</span></a> notify</code></em>);
<a class="link" href="glib-The-Main-Event-Loop.html#GSource" title="struct GSource"><span class="returnvalue">GSource</span></a> *           <a class="link" href="glib-UNIX-specific-utilities-and-integration.html#g-unix-signal-source-new" title="g_unix_signal_source_new ()">g_unix_signal_source_new</a>            (<em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> signum</code></em>);
<a class="link" href="glib-Basic-Types.html#gboolean" title="gboolean"><span class="returnvalue">gboolean</span></a>            <a class="link" href="glib-UNIX-specific-utilities-and-integration.html#g-unix-set-fd-nonblocking" title="g_unix_set_fd_nonblocking ()">g_unix_set_fd_nonblocking</a>           (<em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> fd</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gboolean" title="gboolean"><span class="type">gboolean</span></a> nonblock</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a> **error</code></em>);
</pre>
</div>
<div class="refsect1">
<a name="glib-UNIX-specific-utilities-and-integration.description"></a><h2>Description</h2>
<p>
Most of GLib is intended to be portable; in contrast, this set of
functions is designed for programs which explicitly target UNIX,
or are using it to build higher level abstractions which would be
conditionally compiled if the platform matches G_OS_UNIX.
</p>
<p>
To use these functions, you must explicitly include the
"glib-unix.h" header.
</p>
</div>
<div class="refsect1">
<a name="glib-UNIX-specific-utilities-and-integration.details"></a><h2>Details</h2>
<div class="refsect2">
<a name="G-UNIX-ERROR:CAPS"></a><h3>G_UNIX_ERROR</h3>
<pre class="programlisting">#define G_UNIX_ERROR (g_unix_error_quark())
</pre>
<p>
Error domain for API in the "g_unix_" namespace.  Note that there is
no exported enumeration mapping <code class="literal">errno</code>.  Instead, all functions
ensure that <code class="literal">errno</code> is relevant.  The code for all <a class="link" href="glib-UNIX-specific-utilities-and-integration.html#G-UNIX-ERROR:CAPS" title="G_UNIX_ERROR"><span class="type">G_UNIX_ERROR</span></a> is
always <code class="literal">0</code>, and the error message is always
generated via <a class="link" href="glib-String-Utility-Functions.html#g-strerror" title="g_strerror ()"><code class="function">g_strerror()</code></a>.
</p>
<p>
It is expected that most code will not look at <code class="literal">errno</code> from these
APIs. Important cases where one would want to differentiate between
errors are already covered by existing cross-platform GLib API,
such as e.g. <a href="./../gio/gio/GFile.html"><span class="type">GFile</span></a> wrapping <code class="literal">ENOENT</code>.  However, it is provided for
completeness, at least.
</p>
</div>
<hr>
<div class="refsect2">
<a name="g-unix-open-pipe"></a><h3>g_unix_open_pipe ()</h3>
<pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gboolean" title="gboolean"><span class="returnvalue">gboolean</span></a>            g_unix_open_pipe                    (<em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> *fds</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> flags</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a> **error</code></em>);</pre>
<p>
Similar to the UNIX <code class="function">pipe()</code> call, but on modern systems like Linux
uses the <code class="function">pipe2()</code> system call, which atomically creates a pipe with
the configured flags.  The only supported flag currently is
<code class="literal">FD_CLOEXEC</code>.  If for example you want to configure <code class="literal">O_NONBLOCK</code>,
that must still be done separately with <code class="function">fcntl()</code>.
</p>
<p>
</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>This function does *not* take <code class="literal">O_CLOEXEC</code>, it takes
<code class="literal">FD_CLOEXEC</code> as if for <code class="function">fcntl()</code>; these are different on
Linux/glibc.</div>
<p>
</p>
<div class="variablelist"><table border="0">
<col align="left" valign="top">
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>fds</code></em> :</span></p></td>
<td>Array of two integers</td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>flags</code></em> :</span></p></td>
<td>Bitfield of file descriptor flags, see "man 2 fcntl"</td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>error</code></em> :</span></p></td>
<td>a <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a>
</td>
</tr>
<tr>
<td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
<td>
<a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> on success, <a class="link" href="glib-Standard-Macros.html#FALSE:CAPS" title="FALSE"><code class="literal">FALSE</code></a> if not (and errno will be set).</td>
</tr>
</tbody>
</table></div>
<p class="since">Since 2.30</p>
</div>
<hr>
<div class="refsect2">
<a name="g-unix-signal-add"></a><h3>g_unix_signal_add ()</h3>
<pre class="programlisting"><a class="link" href="glib-Basic-Types.html#guint" title="guint"><span class="returnvalue">guint</span></a>               g_unix_signal_add                   (<em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> signum</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-The-Main-Event-Loop.html#GSourceFunc" title="GSourceFunc ()"><span class="type">GSourceFunc</span></a> handler</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gpointer" title="gpointer"><span class="type">gpointer</span></a> user_data</code></em>);</pre>
<p>
A convenience function for <a class="link" href="glib-UNIX-specific-utilities-and-integration.html#g-unix-signal-source-new" title="g_unix_signal_source_new ()"><code class="function">g_unix_signal_source_new()</code></a>, which
attaches to the default <a class="link" href="glib-The-Main-Event-Loop.html#GMainContext" title="GMainContext"><span class="type">GMainContext</span></a>.  You can remove the watch
using <a class="link" href="glib-The-Main-Event-Loop.html#g-source-remove" title="g_source_remove ()"><code class="function">g_source_remove()</code></a>.
</p>
<div class="variablelist"><table border="0">
<col align="left" valign="top">
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>signum</code></em> :</span></p></td>
<td>Signal number</td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>handler</code></em> :</span></p></td>
<td>Callback</td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>user_data</code></em> :</span></p></td>
<td>Data for <em class="parameter"><code>handler</code></em>
</td>
</tr>
<tr>
<td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
<td>An ID (greater than 0) for the event source</td>
</tr>
</tbody>
</table></div>
<p class="since">Since 2.30</p>
</div>
<hr>
<div class="refsect2">
<a name="g-unix-signal-add-full"></a><h3>g_unix_signal_add_full ()</h3>
<pre class="programlisting"><a class="link" href="glib-Basic-Types.html#guint" title="guint"><span class="returnvalue">guint</span></a>               g_unix_signal_add_full              (<em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> priority</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> signum</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-The-Main-Event-Loop.html#GSourceFunc" title="GSourceFunc ()"><span class="type">GSourceFunc</span></a> handler</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gpointer" title="gpointer"><span class="type">gpointer</span></a> user_data</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-Datasets.html#GDestroyNotify" title="GDestroyNotify ()"><span class="type">GDestroyNotify</span></a> notify</code></em>);</pre>
<p>
A convenience function for <a class="link" href="glib-UNIX-specific-utilities-and-integration.html#g-unix-signal-source-new" title="g_unix_signal_source_new ()"><code class="function">g_unix_signal_source_new()</code></a>, which
attaches to the default <a class="link" href="glib-The-Main-Event-Loop.html#GMainContext" title="GMainContext"><span class="type">GMainContext</span></a>.  You can remove the watch
using <a class="link" href="glib-The-Main-Event-Loop.html#g-source-remove" title="g_source_remove ()"><code class="function">g_source_remove()</code></a>.
</p>
<div class="variablelist"><table border="0">
<col align="left" valign="top">
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>priority</code></em> :</span></p></td>
<td>the priority of the signal source. Typically this will be in
the range between <a class="link" href="glib-The-Main-Event-Loop.html#G-PRIORITY-DEFAULT:CAPS" title="G_PRIORITY_DEFAULT"><span class="type">G_PRIORITY_DEFAULT</span></a> and <a class="link" href="glib-The-Main-Event-Loop.html#G-PRIORITY-HIGH:CAPS" title="G_PRIORITY_HIGH"><span class="type">G_PRIORITY_HIGH</span></a>.</td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>signum</code></em> :</span></p></td>
<td>Signal number</td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>handler</code></em> :</span></p></td>
<td>Callback</td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>user_data</code></em> :</span></p></td>
<td>Data for <em class="parameter"><code>handler</code></em>
</td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>notify</code></em> :</span></p></td>
<td>
<a class="link" href="glib-Datasets.html#GDestroyNotify" title="GDestroyNotify ()"><span class="type">GDestroyNotify</span></a> for <em class="parameter"><code>handler</code></em>
</td>
</tr>
<tr>
<td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
<td>An ID (greater than 0) for the event source</td>
</tr>
</tbody>
</table></div>
<p class="since">Since 2.30</p>
</div>
<hr>
<div class="refsect2">
<a name="g-unix-signal-source-new"></a><h3>g_unix_signal_source_new ()</h3>
<pre class="programlisting"><a class="link" href="glib-The-Main-Event-Loop.html#GSource" title="struct GSource"><span class="returnvalue">GSource</span></a> *           g_unix_signal_source_new            (<em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> signum</code></em>);</pre>
<p>
Create a <a class="link" href="glib-The-Main-Event-Loop.html#GSource" title="struct GSource"><span class="type">GSource</span></a> that will be dispatched upon delivery of the UNIX
signal <em class="parameter"><code>signum</code></em>.  Currently only <code class="literal">SIGHUP</code>, <code class="literal">SIGINT</code>, and <code class="literal">SIGTERM</code> can
be monitored.  Note that unlike the UNIX default, all sources which
have created a watch will be dispatched, regardless of which
underlying thread invoked <a class="link" href="glib-UNIX-specific-utilities-and-integration.html#g-unix-signal-source-new" title="g_unix_signal_source_new ()"><code class="function">g_unix_signal_source_new()</code></a>.
</p>
<p>
For example, an effective use of this function is to handle SIGTERM
cleanly; flushing any outstanding files, and then calling
<a class="link" href="glib-The-Main-Event-Loop.html#g-main-loop-quit" title="g_main_loop_quit ()"><code class="function">g_main_loop_quit()</code></a>.  It is not safe to do any of this a regular
UNIX signal handler; your handler may be invoked while <code class="function">malloc()</code> or
another library function is running, causing reentrancy if you
attempt to use it from the handler.  None of the GLib/GObject API
is safe against this kind of reentrancy.
</p>
<p>
The interaction of this source when combined with native UNIX
functions like <code class="function">sigprocmask()</code> is not defined.
</p>
<p>
</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>For reliable behavior, if your program links to gthread
(either directly or indirectly via GObject, GIO, or a higher level
library), you should ensure <a class="link" href="glib-Threads.html#g-thread-init" title="g_thread_init ()"><code class="function">g_thread_init()</code></a> is called before using
this function.  For example, if your program uses GObject, call
<a href="./../gobject/gobject/gobject-Type-Information.html#g-type-init"><code class="function">g_type_init()</code></a>.</div>
<p>
</p>
<p>
The source will not initially be associated with any <a class="link" href="glib-The-Main-Event-Loop.html#GMainContext" title="GMainContext"><span class="type">GMainContext</span></a>
and must be added to one with <a class="link" href="glib-The-Main-Event-Loop.html#g-source-attach" title="g_source_attach ()"><code class="function">g_source_attach()</code></a> before it will be
executed.
</p>
<div class="variablelist"><table border="0">
<col align="left" valign="top">
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>signum</code></em> :</span></p></td>
<td>A signal number</td>
</tr>
<tr>
<td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
<td>A newly created <a class="link" href="glib-The-Main-Event-Loop.html#GSource" title="struct GSource"><span class="type">GSource</span></a>
</td>
</tr>
</tbody>
</table></div>
<p class="since">Since 2.30</p>
</div>
<hr>
<div class="refsect2">
<a name="g-unix-set-fd-nonblocking"></a><h3>g_unix_set_fd_nonblocking ()</h3>
<pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gboolean" title="gboolean"><span class="returnvalue">gboolean</span></a>            g_unix_set_fd_nonblocking           (<em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gint" title="gint"><span class="type">gint</span></a> fd</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-Basic-Types.html#gboolean" title="gboolean"><span class="type">gboolean</span></a> nonblock</code></em>,
                                                         <em class="parameter"><code><a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a> **error</code></em>);</pre>
<p>
Control the non-blocking state of the given file descriptor,
according to <em class="parameter"><code>nonblock</code></em>.  On most systems this uses <code class="literal">O_NONBLOCK</code>, but
on some older ones may use <code class="literal">O_NDELAY</code>.
</p>
<div class="variablelist"><table border="0">
<col align="left" valign="top">
<tbody>
<tr>
<td><p><span class="term"><em class="parameter"><code>fd</code></em> :</span></p></td>
<td>A file descriptor</td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>nonblock</code></em> :</span></p></td>
<td>If <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a>, set the descriptor to be non-blocking</td>
</tr>
<tr>
<td><p><span class="term"><em class="parameter"><code>error</code></em> :</span></p></td>
<td>a <a class="link" href="glib-Error-Reporting.html#GError" title="struct GError"><span class="type">GError</span></a>
</td>
</tr>
<tr>
<td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
<td>
<a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> if successful</td>
</tr>
</tbody>
</table></div>
<p class="since">Since 2.30</p>
</div>
</div>
</div>
<div class="footer">
<hr>
          Generated by GTK-Doc V1.17</div>
</body>
</html>